// pages/storeObligation/storeObligation.js
const app = getApp();
import api from '../../../utils/util.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess:{
      navtitle: "收银台",
      backhide: 0,
      topheight: 1,
    },
    //倒计时
    endTime: 0,
    demolist:{
      "orderNo": "W202006060901",
      "needPay": "860.00",
      "vipLast": "68000.00",
      "giveMon": "3000.00",
      "canUse": "40.00"
    },
    // 优惠券抵扣
    demolist2:{
      "redPoint": 3,
      "name": "新手300元现金券",
      "deduct": "150.00",
      "deductname": "优惠券"
    },
    // 积分抵扣
    demolist3:{
      "integral": 58000,
      "integraldiscount": "58.00",
      "paytype": "积分",
      "integraldiscount": "58"
    }
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  // 倒计时
  countDown:function(){
    var that=this;
    var nowTime = new Date().getTime();//现在时间（时间戳）
    var time = (that.data.endTime-nowTime)/1000;//距离结束的毫秒数
    // 获取天、时、分、秒
    let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    day = that.timeFormin(day),
    hou = that.timeFormin(hou),
    min = that.timeFormin(min),
    sec = that.timeFormin(sec)
    that.setData({
      day: that.timeFormat(day),
      hou: that.timeFormat(hou),
      min: that.timeFormat(min),
      sec: that.timeFormat(sec)
    })
    // console.log(day + "," + hou + "," + min + "," + sec)
    //每1000ms刷新一次
    if (time>0){
      that.setData({
        countDown: true
      })
      setTimeout(this.countDown, 1000);
    }else{
      that.setData({
        countDown:false
      })
    }
  },
  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数（不会出现负数）
  timeFormin(param) {
    return param < 0 ? 0: param;
  },
  // 商品支付成功弹窗
  storePaySuccess:function(){
    var that = this;
    that.setData({
      storePayedflag: true,
      toStorePayedObj:{
        "No": "2132321421412342"
      }
    })
  },
  closefun: function(){
    var that =this;
    that.setData({
      storePayedflag: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 模拟30分钟倒计时
    var nowTime = new Date().getTime(),//现在时间（时间戳）
        endTime = nowTime + 1800000;
    console.log(nowTime,endTime);
    that.setData({
      endTime: endTime
    })
    this.countDown();
    var token = wx.getStorageSync('token');
    that.setData({
      ent: app.globalData.ent,
      storePayedflag: false,
      token: token
    })
    var goods = JSON.stringify({
      "goods": JSON.parse(options.message).goods
    })
    console.log(typeof(goods),goods,that.data.ent);
    api.post("/mini/my/pay/cartDoOrder",{
      "token": token,
      "ent": that.data.ent,
      "goods": goods,
      "receiveSmallArea": 21,
      "receiveAddress": "31",
      "userName": "231",
      "phone": "31231",
      "remark": "312"
    }).then(res => {
      console.log(res)
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})